﻿@page "/OA_Manage/OA_DefForm/List"

@inherits BaseListWithEdit<OA_DefFormDTO, EditForm>

<Space Size="@("small")">

    @if (Operator.HasPerm("OA_DefForm.Add"))
    {
        <SpaceItem>
            <Button Type="@ButtonType.Primary" Icon="plus" OnClick="()=>Edit()">新建</Button>
        </SpaceItem>
    }
    @if (Operator.HasPerm("Quartz_Task.Edit"))
    {
        <SpaceItem>
            <Button Type="@ButtonType.Primary" Icon="plus" OnClick="()=>Stop()" Disabled=@NoneSelectedItems>停用</Button>
        </SpaceItem>
        <SpaceItem>
            <Button Type="@ButtonType.Primary" Icon="plus" OnClick="()=>Start()" Disabled=@NoneSelectedItems>开启</Button>
        </SpaceItem>
    }
    @if (Operator.HasPerm("OA_DefForm.Delete"))
    {
        <SpaceItem>
            <Popconfirm Title="确认删除选中项吗?"
                    OnConfirm="()=>Delete()"
                    OnCancel="()=>{}"
                    OkText="确定"
                    CancelText="取消"
                    Disabled=@NoneSelectedItems>
                <Button Type="@ButtonType.Primary" Danger Icon="minus" Disabled=@NoneSelectedItems>删除</Button>
            </Popconfirm>
        </SpaceItem>
    }
    <SpaceItem>
        <SimpleSelect DefaultActiveFirstOption @bind-Value="@Condition">
            <SelectOptions>
                <SimpleSelectOption Value="Type" Label="分类"></SimpleSelectOption>
                <SimpleSelectOption Value="Name" Label="标题"></SimpleSelectOption>
                <SimpleSelectOption Value="Text" Label="摘要"></SimpleSelectOption>
                <SimpleSelectOption Value="CreatorName" Label="创建者"></SimpleSelectOption>
                <SimpleSelectOption Value="Status" Label="状态"></SimpleSelectOption>
            </SelectOptions>
        </SimpleSelect>
    </SpaceItem>
    <SpaceItem>
        <Search Placeholder="关键字" EnterButton="true" @bind-Value="@KeyWord" OnSearch="()=>Refresh()" />
    </SpaceItem>
</Space>

<Table @ref="_table"
       TItem="OA_DefFormDTO"
       DataSource="Data"
       EnableVirtualization
       Loading="Loading"
       @bind-PageSize="Pagination.PageRows"
       @bind-SelectedRows="SelectedItems"
       ScrollX="1400"
       ScrollBarWidth="12px"
       Size="TableSize.Small"
       RowClassName="@(x => x.RowIndex % 2 == 0 ?"gray-2":"")">
    <ChildContent>
        <Selection Key="@(context.Id)" />
        <AntDesign.Column Title="分类" DataIndex="Type" TData="System.String" />
        <AntDesign.Column Title="标题" DataIndex="Name" TData="System.String" />
        <AntDesign.Column Title="摘要" DataIndex="Text" TData="System.String" />
        <AntDesign.Column Title="创建者" DataIndex="CreatorName" TData="System.String" />
        <AntDesign.Column Title="状态" TData="System.String">
            @if (@context.Status == 0)
            {
                <Badge Color="Red" Text="停止" />
            }
            else
            {
                <Badge Color="#87d068" Text="启动" />
            }
        </AntDesign.Column>
        <AntDesign.Column Title="权限角色" TData="System.String">
           @if (context.ValueRoles?.Count() > 0)
           {
                @foreach (var item in Roles)
                {
                    @if (@context.ValueRoles.Contains(item.Value))
                    {
                        <Tag Color="success">
                            @item.Text                      
                        </Tag>
                    }
                }
           }
        </AntDesign.Column>
        <ActionColumn Title="Action" Fixed="right">
            <Space Size=@("small")>
                @if (Operator.HasPerm("OA_DefForm.Edit"))
                {
                    <SpaceItem>
                        <Button Type="@ButtonType.Link" Style="padding:0px" OnClick="()=>Edit(context)">Edit</Button>
                    </SpaceItem>
                }
                @if (Operator.HasPerm("OA_DefForm.Delete"))
                {
                    <SpaceItem>
                        <Popconfirm Title="确认删除吗?"
                                OnConfirm="()=>Delete(context.Id)"
                                OnCancel="()=>{}"
                                OkText="确定"
                                CancelText="取消">
                            <Button Danger Type="@ButtonType.Link" Style="padding:0px">Delete</Button>
                        </Popconfirm>
                    </SpaceItem>
                }
            </Space>
        </ActionColumn>
    </ChildContent>
    <PaginationTemplate>
        <div style="float:right;margin-top:10px">
            <Pagination Total="Pagination.Total"
                        ShowTotal="ShowTotal"
                        ShowSizeChanger
                        PageSize="Pagination.PageRows"
                        Current="Pagination.PageIndex"
                        OnChange="PageIndexChanged"
                        OnShowSizeChange="PageSizeChanged" />
        </div>
    </PaginationTemplate>
</Table>

<style>
    .gray-2 {
        background-color: #fafafa;
    }
</style>

@code
{
    public List()
    {
        Area = "OA_Manage";
        EditWitdh = 1200d;
        NewTitle = "新建流程";
        EditTitle = "编辑流程";
    }

    private List<SelectOption> Roles { get; set; }

    private void Start(string? id = null)
    {
        List<string> ids;
        if (string.IsNullOrEmpty(id))
        {
            ids = SelectedItems.Select(p => p.Id).ToList();
        }
        else
        {
            ids = new List<string> { id };
        }
        Excute("开始", "StartData", ids);
    }

    private void Stop(string? id = null)
    {
        List<string> ids;
        if (string.IsNullOrEmpty(id))
        {
            ids = SelectedItems.Select(p => p.Id).ToList();
        }
        else
        {
            ids = new List<string> { id };
        }
        Excute("停用", "StopData", ids);
    }

    private void Excute(string Text, string Action, List<string> ids)
    {
        Func<ModalClosingEventArgs, Task> onOkClick = async (e) =>
        {
            using (var waitfor = WaitFor.GetWaitFor(this))
            {
                try
                {
                    var result = await DataProvider.PostData<AjaxResult>($"/OA_Manage/OA_DefForm/{Action}", ids.ToJson());
                    if (!result.Success)
                    {
                        throw new MsgException(result.Msg);
                    }
                    await GetData();
                    StateHasChanged();
                }
                catch (Exception ex)
                {
                    await Error.ProcessError(ex);
                }
            }
        };

        RenderFragment icon = (builder) =>
        {
            var index = 0;
            IEnumerable<KeyValuePair<string, object>> paramenter = new List<KeyValuePair<string, object>>()
                                                {
                new KeyValuePair<string, object>("Type","check-circle")
                                                };

            builder.OpenComponent(index++, typeof(Icon));
            builder.AddMultipleAttributes(index++, paramenter);
            builder.CloseComponent();

        };
        ModalService.Confirm(new ConfirmOptions()
            {
                Title = $"任务{Text}",
                Icon = icon,
                Content = $"确认{Text}吗？",
                Centered = true,
                OnOk = onOkClick
            });
    }

    protected override async Task OnInitializedAsync()
    {
        await GetRoles();
        await base.OnInitializedAsync();
    }

    private async Task GetRoles()
    {
        Roles = await UserData.GetBase_Role();
    }
}